C_SOURCES=$(wildcard kernel/*.c drivers/*.c cpu/*.c)
HEADERS=$(wildcard kernel/*.h drivers/*.h)
OBJ=$(C_SOURCES:.c=.o)

# 编译器
# CC=x86_64-elf-gcc -m32 
CC=gcc -m32 -std=c99 -fno-pic 
# LD=x86_64-elf-ld -m elf_i386
LD=ld -m elf_i386
# GDB=x86_64-elf-gdb 
GDB=gdb

# -g: Use debugging symbols in gcc
CFLAGS=-g

os-image.bin: boot/boot_sect.bin kernel.bin
	cat $^ > os-image.bin

# '--oformat binary' deletes all symbols as a collateral, so we don't need
# to 'strip' them manually on this case
kernel.bin:	boot/kernel_entry.o cpu/interrupt.o $(OBJ)
	$(LD) -o $@ -Ttext 0x1000 $^ --oformat binary

# Used for debugging purposes
kernel.elf: boot/kernel_entry.o cpu/interrupt.o ${OBJ}
	$(LD) -o $@ -Ttext 0x1000 $^ 

# Open the connection to qemu and load our kernel-object file with symbols
debug: os-image.bin kernel.elf
	qemu-system-i386 -s -S -fda os-image.bin &
	${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"

%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) -ffreestanding -c $< -o $@

%.o: %.asm
	nasm $< -f elf32 -o $@

%.bin: %.asm
	nasm $< -f bin -o $@

clean:
	rm -rf *.bin *.dis *.o os-image.bin *.elf
	rm -rf kernel/*.o boot/*.bin boot/*.o drivers/*.o cpu/*.o
